package hajimi.java81.model;

import java.sql.*;

public class ddtext {

    String name;
    String pass;
//
    public void setinfo(String username,String password){
        this.name=username;
        this.pass=password;
    }

    public  void main(String[] args) {
        userlogin();
    }

    public  void userlogin() {
        String url = "jdbc:mysql://localhost:3306/library_management?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
        String username = "root"; // 数据库用户名
        String password = "060331"; // 数据库密码
        String sql = "select * from user where username = ? and password = ?";
        int id = 0;
        String email = "";
        Connection conn = null;
        PreparedStatement prep = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动程序
            conn = DriverManager.getConnection(url, username, password);
            String name = "admin";
            String pass = "admin123";

            //数据查询
            prep = conn.prepareStatement(sql);
            prep.setString(1, name);
            prep.setString(2, pass);


            //数据结果
            rs = prep.executeQuery();
            while (rs.next()) {
                id = rs.getInt("id");
                email = rs.getString("email");
            }

        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                rs.close();
            } catch (SQLException e) {
                System.out.println("rs close falid");
                throw new RuntimeException(e);
            }
            try {
                prep.close();
            } catch (SQLException e) {
                System.out.println("prep close falid");
                throw new RuntimeException(e);
            }
            try {
                conn.close();
            } catch (SQLException e) {
                System.out.println("conn close falid");
                throw new RuntimeException(e);
            }
        }
        System.out.println(id);
        System.out.println(email);
    }
}


